<?php

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Admin;
use App\Classs;
use App\Card;
class AdminController extends Controller
{
    public function getLogin(){
        //返回登陆页
        return view('admin/login/login');
    }

    public function postIndex(Request $request){
        //接受用户登录信息
        $username = $request->username;
        $pass = $request->pass;

        //查询用户输入的信息在数据库中是否存在
        $res = Admin::where('username', '=', $username)->where('pass', '=', $pass)->first();
        if ($res) {
            //验证成功后 将所有的数据存到session中
            session(['admin'=>$res]);
            return redirect('admin/index');
        } else {
            return back()->with('error','账号或密码错误');
        }
    }

    public function getIndex(Request $request){
        //名片列表页
        if($request->sname) {
            //根据用户搜索的名字关键字查询用户所有的名片
            $data = Card::where('name','like','%' . $request->sname . '%')->simplePaginate(2);
            return view('admin/index/list',['data'=>$data]);
        } elseif($request->scompany) {
            //根据用户搜索的公司名关键字查询用户所有的名片
            $data = Card::where('company','like','%' . $request->scompany . '%')->simplePaginate(2);
            return view('admin/index/list',['data'=>$data]);
        } else {
            //查询session中admin的值
            $admin = $request->session()->get('admin');
            //根据session中的uid查询用户所有的名片
            $data = Card::where('uid','=',$admin['id'])->simplePaginate(2);
            return view('admin/index/list',['data'=>$data]);
        }

    }
}